package com.zhxh.nzpos.query;

import pl.polidea.treeview.AbstractTreeViewAdapter;
import pl.polidea.treeview.TreeNodeInfo;
import pl.polidea.treeview.TreeStateManager;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.zhxh.nzpos.R;
import com.zhxh.nzpos.model.Statistics;

public class GoodsQueryAdapter extends AbstractTreeViewAdapter<Statistics> {

	private LayoutInflater inflater;
	private Activity activity;

	public GoodsQueryAdapter(Activity activity,
			TreeStateManager<Statistics> treeStateManager, int numberOfLevels) {
		super(activity, treeStateManager, numberOfLevels);
		inflater = activity.getLayoutInflater();
		this.activity = activity;
	}

	

	@Override
	public View getNewChildView(TreeNodeInfo<Statistics> treeNodeInfo) {
		View v = inflater.inflate(R.layout.query_sum_list_item, null);
		updateView(v, treeNodeInfo);
		return v;
	}

	@Override
	public View updateView(View view, TreeNodeInfo<Statistics> treeNodeInfo) {
		 final Statistics statistics = (Statistics) treeNodeInfo.getId();
		 ((TextView) view.findViewById(R.id.static_name))
			.setText(statistics.getName());
		 
		 ((TextView) view.findViewById(R.id.static_price))
			.setText(statistics.getPrice());
		 
		 
		 return view;
	}

	@Override
	public Drawable getBackgroundDrawable(
			final TreeNodeInfo<Statistics> treeNodeInfo) {
		switch (treeNodeInfo.getLevel()) {
		case 0:
			return getActivity().getResources().getDrawable(
					R.drawable.list_title_bg);
		case 1:
			 return new ColorDrawable(Color.parseColor("#dbdbdb"));
		default:
			return null;
		}
	}



	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}
	  @Override
	    public void handleItemClick(final View view, final Object id) {
	        final Statistics statistics = (Statistics) id;
	        final TreeNodeInfo<Statistics> info = getManager().getNodeInfo(statistics);
	        if(info.getLevel() == 1) {
	        	Intent intent = new Intent();
				intent.setClass(activity,
						QueryLevel2Activity.class);
				Bundle b = new Bundle();
				
				b.putSerializable("cate", statistics);
				intent.putExtras(b);
				activity.startActivity(intent);
				//startActivity(intent);
	        }
	/*        if (info.isWithChildren()) {
	            super.handleItemClick(view, id);
	        } else {
	            final ViewGroup vg = (ViewGroup) view;
//	            final CheckBox cb = (CheckBox) vg
//	                    .findViewById(R.id.demo_list_checkbox);
//	            cb.performClick();
	           Toast.makeText(getActivity(), "id="+category.getLevel(), Toast.LENGTH_SHORT).show();
	        }*/
	  }
}
